﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Globalization;

namespace TipCalc
{
    public partial class MainPage : PhoneApplicationPage
    {
        private float m_amout = 0.0f;
        private float m_precentTip = 10.0f;
        private int m_people = 1;
        private SolidColorBrush m_SolidColorBrush = new SolidColorBrush(Colors.Red);
        private bool m_onNavigatedToCalled = true;

        /// <summary>
        /// Constructor
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);
        }

        private void MainPage_LayoutUpdated(object sender, EventArgs e)
        {
            if (m_onNavigatedToCalled)
            {
                m_onNavigatedToCalled = false;
                Dispatcher.BeginInvoke(() =>
                {
                    Random random = new Random();
                    var number = random.Next(1, 3);
                    string fileName = String.Format(@"Images\background{0}.jpg", number);
                    backgroundBrush.ImageSource = new BitmapImage(new Uri(fileName, UriKind.Relative));
                    radioButton3.IsChecked = true;
                    mainInAnimation.Begin();
                });
            }
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            {
                Grid.SetRow(bottomGrid, 1);
                Grid.SetColumn(bottomGrid, 0);
                sumStackPanel.Margin = new Thickness(35, 30, 0, 0);
            }
            else
            {
                Grid.SetRow(bottomGrid, 0);
                Grid.SetColumn(bottomGrid, 1);
                sumStackPanel.Margin = new Thickness(-5, 30, 0, 0);
            }
        }

        private void amountTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (float.TryParse(amountTextBox.Text, NumberStyles.Any, CultureInfo.CurrentCulture, out m_amout))
            {
                amountTextBox.Foreground = ((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]);
                this.Calculate();
            }
            else
            {
                if (!String.IsNullOrEmpty(amountTextBox.Text))
                {
                    amountTextBox.Foreground = m_SolidColorBrush;
                }
            }
            translateAmountAnimation.Begin();
        }

        private void percentTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (float.TryParse(percentTextBox.Text, NumberStyles.Any, CultureInfo.CurrentCulture, out m_precentTip))
            {
                percentTextBox.Foreground = ((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]);
                this.Calculate();
            }
            else
            {
                percentTextBox.Foreground = m_SolidColorBrush;
            }
            percentAmountAnimation.Begin();
        }

        private void peopleTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(peopleTextBox.Text, out m_people))
            {
                peopleTextBox.Foreground = ((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]);
                this.Calculate();
            }
            else
            {
                peopleTextBox.Foreground = m_SolidColorBrush;
            }
            peopleAmountAnimation.Begin();
        }

        private void Calculate()
        {
            var tip = m_amout * 0.01f * m_precentTip;
            var total = tip + m_amout;
            tipAmountTextBlock.Text = tip.ToString("C");
            totalPayAmountTextBlock.Text = total.ToString("C");
            totalPerPersonTextBlock.Text = (total / m_people).ToString("C");
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            m_precentTip = 0.0f;
            percentTextBox.Text = "0";
            this.Calculate();
        }

        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            m_precentTip = 5.0f;
            percentTextBox.Text = "5";
            this.Calculate();
        }

        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            m_precentTip = 10.0f;
            percentTextBox.Text = "10";
            this.Calculate();
        }

        private void radioButton4_Checked(object sender, RoutedEventArgs e)
        {
            m_precentTip = 15.0f;
            percentTextBox.Text = "15";
            this.Calculate();
        }

        private void radioButton5_Checked(object sender, RoutedEventArgs e)
        {
            m_precentTip = 20.0f;
            percentTextBox.Text = "20";
            this.Calculate();
        }

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            m_precentTip = 10.0f;
            percentTextBox.Text = "10";
            m_amout = 0.0f;
            amountTextBox.Text = "";
            m_people = 1;
            peopleTextBox.Text = "1";
            radioButton3.IsChecked = true;
            this.Calculate();
        }

        private void amountTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            translateAmountAnimation.Begin();
        }

        private void percentTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            percentAmountAnimation.Begin();
        }

        private void peopleTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            peopleAmountAnimation.Begin();
        }

        private void aboutButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Tip Calc was made by Lukas Madon.\nYou can send complaints or wishes to lukasz.madon@gmail.com. \nCheck out my other games and apps!\nSpirit Level, Bomber and more!");
        }
    }
}